import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.List;
import java.util.Vector;

import com.google.gdata.client.projecthosting.IssuesQuery;
import com.google.gdata.client.projecthosting.ProjectHostingService;
import com.google.gdata.data.HtmlTextConstruct;
import com.google.gdata.data.Person;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.projecthosting.Id;
import com.google.gdata.data.projecthosting.IssueCommentsEntry;
import com.google.gdata.data.projecthosting.IssueCommentsFeed;
import com.google.gdata.data.projecthosting.IssuesEntry;
import com.google.gdata.data.projecthosting.IssuesFeed;
import com.google.gdata.data.projecthosting.Label;
import com.google.gdata.data.projecthosting.MergedInto;
/*ISSUE ID : 51605
Merged:51325
Str=insert into dupDetails values('51325','51605')
i=11749*/

/*Desc:This Class is used to create dataset of duplicate bug reports
 * */
public class DownloadMergeInfo 
{
	String file_path = "D:\\Research\\Rshabarth\\Results\\duplicate_chromium_issue_report_exp_data.csv";
	
	
	
    private String project = "chromium";	
	private Connection conn = null;
	private Statement stmt = null;
    private String url = "jdbc:mysql://localhost:3306/";
    private String dbName ="lsi_python" ;
    private String driver = "com.mysql.jdbc.Driver";
    private String userName = "root";
    private String password = "123";
    private String dupdetails_table = "dupDetails"; //Table that store Information main issues and their duplicates
    private String Dup_Table = "duplicateissues";/*Table that will store information about duplicate for whhihc we are able to get main Issue 
                                  Inofmration**/   
    private ProjectHostingService myService = null;
    private preprocess pobj =  new preprocess();
    Vector<String>DupIssues= new Vector<String>();
    Vector<DupClass> MainAndDupIssues;
    Vector<String> DupHavingMergeInfo = new Vector<String>();

	
public void getMergeTo(ProjectHostingService service, String DupIssueId){
		try{ 
			URL feedUrl = new URL("https://code.google.com/feeds/issues/p/chromium/issues/full/" + DupIssueId);
			IssuesEntry entry = service.getEntry(feedUrl, IssuesEntry.class);
			
			System.out.println("-------------------------------------------------------------");
			Id id = entry.getIssueId();
			int issueID = id.getValue();
			List<Label> labels =  entry.getLabels();
			for(int i=0;i<labels.size();i++){
				Label l = labels.get(i);
				l.getValue();
				//System.out.println("i="+labels.get(i));
			}
		
			String state = entry.getState().getValue().name();
			String status = entry.getStatus().getValue();
			String MainIssue = entry.getMergedInto().getId().getValue().toString();
					System.out.println("*************************************************************");
		    		System.out.println("ISSUE ID : " + issueID);
		    		//System.out.println("STATE : " + state);
		    		//System.out.println("STATUS : " + status);
		    	    System.out.println("Merged:"+MainIssue);
		    		if(MainIssue!=null)
		    		{
		    			//InsertToMainAndDupIssues(MainIssue.getValue().toString(),DupIssueId);
		    			//DupHavingMergeInfo.add(DupIssueId);
		    			String insertStr = "insert into "+ dupdetails_table +" values('"+MainIssue+"',"+"'"+DupIssueId+"')";
		    			System.out.println("Str="+insertStr);
		    			stmt.executeUpdate(insertStr);
		    			
		    		}
		  
		}catch(Exception e){
			System.out.println(e);
			//interupt();
		}
	}


	  
private void InsertToMainAndDupIssues(String NewMainIssue, String NewDupIssueId)
{
boolean flag = false;


for(int i=0;i<MainAndDupIssues.size();i++)
 {
   if(MainAndDupIssues.get(i).mainIssue.equals(NewMainIssue))
   {
	   MainAndDupIssues.get(i).duplicate.add(NewDupIssueId);
	   flag=true;
	   break;
   }
 }
	
if(flag!=true)
{
  DupClass obj =  new DupClass(NewMainIssue,NewDupIssueId);	
  MainAndDupIssues.add(obj);
}

}

public void process()
{
	try{
			myService = new ProjectHostingService("Sample Application");			
			for(int i=11749;i<DupIssues.size();i++)
			{
				System.out.println("i="+i);
				String Issue = DupIssues.get(i);
				getMergeTo(myService, Issue); 
				
			}
			
		}catch(Exception e){
			System.out.println(e);
	}
}
	
	public void interupt()
	{
		
		BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Intr");
		try {
			br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	///This function will read Ids from CSV file
	private void readIds() {
		// TODO Auto-generated method stub
		
		try 
		{
			BufferedReader br =  new BufferedReader(new FileReader(file_path));
			int count = 0;
			String Issue;
			while((Issue=br.readLine())!=null)
			{
				
				
					Issue = Issue.split(",")[0];
					System.out.println("count="+(++count)+" Isu="+Issue);
					DupIssues.add(Issue);
					//interupt();
				
			}//whlile
				
		
		} catch (FileNotFoundException e) 
		{		e.printStackTrace();}
		
		catch (IOException e)
		{ e.printStackTrace();}
		
		System.out.println("Count="+DupIssues.size());
	}	
	
	/*

private void InsertToDb() 
{
	try 
	{
		Statement stmt =  conn.createStatement();
	    
	for(int i=0;i<MainAndDupIssues.size();i++)
	{
		String AllDuplicates = MainAndDupIssues.get(i).mainIssue;
		for(int j=0;j<MainAndDupIssues.get(i).duplicate.size();j++ )
		{
		 AllDuplicates = AllDuplicates +","+MainAndDupIssues.get(i).duplicate.get(j);
		 
		}
		
		String InsertStr = "Insert into  "+TABLE+" Values("+MainAndDupIssues.get(i)+","+AllDuplicates+")";
		stmt.executeUpdate(InsertStr);
	}//for
	
	
	
	for(int i=0;i<DupHavingMergeInfo.size();i++)
	{
		String InsertStr = "Insert into  "+Dup_Table+" Values("+DupHavingMergeInfo.get(i)+")";
		stmt.executeUpdate(InsertStr);
	}
	
	stmt.close();
	} catch (SQLException e)
	{ 	e.printStackTrace(); 	}

}*/

	
	
//Main Class
  public static void main(String args[])
   {
	  DownloadMergeInfo cdd =  new DownloadMergeInfo();
	   db_conn db =  new db_conn();
	   cdd.conn=db.initdb(cdd.dbName, cdd.dupdetails_table, cdd.userName, cdd.password); 
	   try 
	   {
		cdd.stmt = cdd.conn.createStatement();
	  } catch (SQLException e1) 
	  { e1.printStackTrace(); }
	  
	   cdd.readIds();
	   cdd.process();
	
	  // cdd.InsertToDb();
	   try 
	   {
		cdd.stmt.close();
	   } catch (SQLException e)
	   { e.printStackTrace(); }
	   
	   db.closedb(cdd.conn);
	   
   }//main





}//Class
